package com.lidroid.xutils_sample.core;

import com.lidroid.xutils.bitmap.core.FileNameGenerator;
import org.junit.Test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static org.junit.Assert.*;

public class FileNameGeneratorTest {

    @Test
    public void generator() throws NoSuchAlgorithmException {
        String key = FileNameGenerator.generator("key");

        String cacheKey;
        final MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
        mDigest.update("key".getBytes());
        cacheKey = bytesToHexString(mDigest.digest());

        assertEquals(cacheKey, key);
    }

    private static String bytesToHexString(byte[] bytes) {

        final int iByte = 0xFF;

        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(iByte & aByte);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}